package com.rzx.mybackend.filter;

import com.rzx.mybackend.service.TokenService;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;

/**
 * JwtAuthenticationTokenFilter
 *
 * @author renzhexian
 * @date 2025/7/15
 */
@Component
@RequiredArgsConstructor
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    private final TokenService tokenService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        // 获取请求路径
        String path = request.getRequestURI();

        // 检查路径是否是登录接口或其他白名单路径
        if (path.startsWith("/login") || path.startsWith("/user/register") || path.startsWith("/user/verify") || path.startsWith("/game/list" ) || path.startsWith("/userlogout" ) || path.startsWith("/push" ) || path.startsWith("/game/getGameDetails") || path.startsWith("/comment/getComment")) {
            // 如果路径是白名单中的接口，直接放行
            filterChain.doFilter(request, response);
            return;
        }
            // 认证token
            tokenService.verifyToken(request);

        // 如果验证通过，继续执行过滤链
        filterChain.doFilter(request, response);
    }
}
